package com.example.asuper.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Quiz extends Activity {
    private static final String TAG = "QUIZ";
    private static final String KEY_INDEX = "index";
    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNext;
    private Button mLast;
    private Button mCheat;
    private Button mHid;
    private TextView mTbQuestion;
    private int currentIndex = 0;
    private int hidNum = 0;
    private boolean mIsCheater;
    private TrueFalse[] message = new TrueFalse[]{
            new TrueFalse(R.string.question_1, true),
            new TrueFalse(R.string.question_2, false),
            new TrueFalse(R.string.question_3, false)
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setTitle(R.string.true_button);
        Log.d(TAG, "OnCreate Method start");
        currentIndex = savedInstanceState != null ? savedInstanceState.getInt(KEY_INDEX) : 0;
        setContentView(R.layout.activity_quiz);
        mTrueButton = (Button) findViewById(R.id.bt_quiz_true);
        mFalseButton = (Button) findViewById(R.id.bt_quiz_false);
        mNext = (Button) findViewById(R.id.bt_next);
        mLast = (Button) findViewById(R.id.bt_last);
        mCheat = (Button) findViewById(R.id.bt_cheat);
        mHid = (Button) findViewById(R.id.btn_hid);
        mTbQuestion = (TextView) findViewById(R.id.tb_quizshow);
        mCheat.setVisibility(View.INVISIBLE);

        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });

        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });
        mLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex--;
                if (currentIndex < 0) {
                    currentIndex = message.length - 1;
                }
                updateQuestion();
            }
        });
        mNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex++;
                updateQuestion();
            }
        });
        mCheat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Quiz.this, Cheat.class);
                intent.putExtra("cheat", message[currentIndex].getQuestion() + ":" + message[currentIndex].isTrueQuestion());
                //startActivity(intent);
                startActivityForResult(intent, 55);
            }
        });

        mHid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hidNum++;
                if (hidNum == 5) {
                    mCheat.setVisibility(View.VISIBLE);
                    return;
                }
                String showMsg = "还剩" + (5 - hidNum) + "次开启作弊按钮";
                Toast.makeText(Quiz.this, showMsg, Toast.LENGTH_SHORT).show();
            }
        });
        mTbQuestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex++;
                updateQuestion();
            }
        });
        updateQuestion();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent date) {
        if (date == null) {
            return;
        }
        mIsCheater = date.getBooleanExtra("CheatResult", false);
        Toast.makeText(Quiz.this, requestCode + "", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG, "onSaveInstanceState Method start");
        savedInstanceState.putInt(KEY_INDEX, currentIndex);
    }


    private void updateQuestion() {
        currentIndex = (currentIndex) % message.length;
        int tbMessage = message[currentIndex].getQuestion();
        mTbQuestion.setText(tbMessage);
    }

    private void checkAnswer(boolean cuurentCheck) {
        boolean result = message[currentIndex].isTrueQuestion();
        int showMsg = 0;
        if (mIsCheater) {
            showMsg = R.string.judgment_toast;
        } else {
            if (cuurentCheck == result) {
                showMsg = R.string.correct_toast;
            } else {
                showMsg = R.string.incorrect_toast;
            }
        }
        Toast.makeText(Quiz.this, showMsg, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "OnStart  Method start");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "OnPause  Method start");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "OnResume  Method start");
    }

    @Override
    public void onRestart() {
        super.onRestart();
        Log.d(TAG, "OnRestart  Method start");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "OnStop  Method start");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "OnDestroy  Method start");
    }

}
